﻿
using Controller.Model;
using Controller.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ControllerManageServer.ActionModel.Aioi
{
    class AioiActionModelMWU2060 : AioiActionModelAbstract
    {

        /// <summary>
        /// 手环ID
        /// </summary>
        public string UhfRfId { get; set; }
        /// <summary>
        /// 手环ID掩码
        /// </summary>
        public string EpcMask { get; set; }

        /// <summary>
        /// 模式数组
        /// </summary>
        /// <returns></returns>
        public override byte[] ModeArray()
        {
            List<byte> data = new List<byte>();
            data.AddRange(DefaultEncoding.GetBytes("m1"));
            data.AddRange(ByteUtils.BinStrToBytes(FactorScroll.BinValue + FactorColor.Value + FactorSeq.Value + FactorBuzzers.Value));
            data.AddRange(DefaultEncoding.GetBytes("m2"));
            data.AddRange(ByteUtils.BinStrToBytes(ConfirmScroll.BinValue + ConfirmColor.Value + ConfirmSeq.Value + ConfirmBuzzers.Value));
            data.AddRange(DefaultEncoding.GetBytes("m3"));
            data.AddRange(ByteUtils.BinStrToBytes(FnScroll.BinValue + FnColor.Value + FnSeq.Value + FnBuzzers.Value));
            data.AddRange(DefaultEncoding.GetBytes("m4"));
            data.AddRange(ByteUtils.BinStrToBytes("01" + FnDecrease.BinValue + FnKey.BinValue + ConfirmKey.BinValue));

            #region MWU2060新加 m8功能键

            data.AddRange(DefaultEncoding.GetBytes("m8"));
            // m8命令后面跟两个字节16bit,每个字节的前4bit固定是0100,
            // 第一个字节的第3位(低bit0-bit7高)是是否双层确认,低3位未使用,
            // 第二个字节的第3位(低bit0-bit7高)是防错动作,低3位未使用
            // 可参考《MWU UHF RF-ID LightModuleUM10》的(4.3.3. UHF RF-ID 显示器专用模式设定命令)
            data.AddRange(ByteUtils.BinStrToBytes($"0100{EpcDoubleConfirm.BinValue}0000100{ErrorProofAction.BinValue}000"));

            #endregion MWU2060新加 m8功能键

            return data.ToArray();
        }

        /// <summary>
        /// 转换成动作模型
        /// </summary>
        /// <param name="iModel"></param>
        public override void ToActionModel(ActionPacketModel iModel)
        {
            FactorColor = GetLightColor(iModel.ControlModel.LightModel);// 亮灯颜色
            FactorBuzzers = GetDeviceBuzzer(iModel.ControlModel.OpenBuzzer);// 蜂鸣器
            FactorSeq = string.IsNullOrEmpty(iModel.ControlModel.Seq) || iModel.ControlModel.Seq.Length == 0 ? Bin.OFF : Bin.ON;// 是否显示
            ConfirmColor = GetLightColor(iModel.ControlModel.ConfirmLightModel);// 按Confirm后的亮灯颜色
            ConfirmBuzzers = GetDeviceBuzzer(iModel.ControlModel.ConfirmOpenBuzzer);// 按Confirm后的蜂鸣器
            ConfirmSeq = string.IsNullOrEmpty(iModel.ControlModel.ConfirmSeq) || iModel.ControlModel.ConfirmSeq.Length == 0 ? Bin.OFF : Bin.ON;// 按Confirm后是否显示
            ConfirmKey = iModel.ControlModel.IsUsedConfirm ? Func.USED : Func.NOT_USED;// 是否使用Confirm键
            UhfRfId = iModel.ControlModel.UhfRfId;// RF-ID
            EpcMask = iModel.ControlModel.EpcMark;// EPC掩码
        }
    }
}
